/**
 * 商家列表模块
 */
define(function(require, exports, module) {
	"require:nomunge,exports:nomunge,module:nomunge";

	var base = require("../../custom/base/component-base.js");
	
	base.init(base);
	
	base.initSearch();
	
	var validate = require("../../custom/base/component-validate.js");
	
	var async = require("../../custom/base/component-async.js");
	
	var head=require("../../custom/head/module-head.js");

	require("../../custom/base/component-core.js");
	

	
	//初始化函数
	exports.init=function(m){
	
		window.$ldh_shop_photos=m;
		
		base.setFromPage("shopPhotos");
		
		head.init(head,true);

		exports.getShopPic();
	};
	

	//查询商家相册
	exports.getShopPic=function(){

		if(validate.isNull($("#shopId").val())){
			
	         $("#container_photo").html('<div class="ldh-subSuccess text-center"><div class="ldh-subSuctips" style="margin-top:160px;color:#ed5d3a">暂无图片</div><div class="ldh-subSucbtn"><button type="button" onclick=history.back(-1) class="btn-yellow">&lt;返回</button></div></div>');

			return;
		}
		
		var params = {
				shopId :$("#shopId").val()
		};
		
			eval(
					Wind.compile("async", function() {

						var data = $await(async.DDF_AsyncRequest("POST", ctx + "/shopweb/Shop/getShopPic.do", params));
						
						
						if(data["code"]==1){
							
							
							if(data["erpShopImgList"].length==0){
								
					           $("#container_photo").html('<div class="ldh-subSuccess text-center"><div class="ldh-subSuctips" style="margin-top:160px;color:#ed5d3a">暂无图片</div><div class="ldh-subSucbtn"><button type="button" onclick=history.back(-1) class="btn-yellow">&lt;返回</button></div></div>');
							
							}else{
								
								
								var htmlStr="",thumbHtmlStr="";
								
								for(var i=0;i<data["erpShopImgList"].length;i++){
									
									htmlStr+='<p class="dis_none"><span class="SliderPicBorder fl-right"><img src='+fileOpenSourceUrl+data["erpShopImgList"][i]["imageUrl"]+'></span></p>';
									
									thumbHtmlStr+='<li rel='+(i+1)+'><img src='+fileOpenUrl+data["erpShopImgList"][i]["imageUrl"]+'></li>';
									
								}
								
								
								$(".ldh-photo-album").show();
									
								$("#photosNum").html(data["erpShopImgList"].length);
								
								$("#ThumbPic").html(thumbHtmlStr);
								
								$("#ldh-photoAlbum-pic").html(' <div id="aPrev" class="ldh-photoAlbum-cursorL"></div>'+htmlStr+'<div id="aNext" class="ldh-photoAlbum-cursorR"></div>');
								
								//缩略图滚动事件
								$(".jCarouselLite").jCarouselLite({
									btnNext: "#btnNext",
									btnPrev: "#btnPrev",
									scroll: 1,
									speed: 240,
									circular: false,
									visible: 6
								});

								var currentImage;
								var currentIndex = -1;

								//显示大图(参数index从0开始计数)
								function showImage(index){

									//更新当前图片页码
									$(".ldh-photoAlbum-current").html(index + 1);

									//隐藏或显示向左向右鼠标手势
									var len = $('#ldh-photoAlbum-pic img').length;
									if(index == len - 1){
										$("#aNext").hide();
									}else{
										$("#aNext").show();
									}

									if(index == 0){
										$("#aPrev").hide();
									}else{
										$("#aPrev").show();
									}

									//显示大图            
									if(index < $('#ldh-photoAlbum-pic img').length){
										var indexImage = $('#ldh-photoAlbum-pic p')[index];

										//隐藏当前的图
										if(currentImage){
											if(currentImage != indexImage){
												$(currentImage).css('z-index', 2);	
												$(currentImage).fadeOut(0,function(){
													$(this).css({'display':'none','z-index':1})
												});
											}
										}

										//显示用户选择的图
										$(indexImage).show().css({'opacity': 0.4});
										$(indexImage).animate({opacity:1},{duration:200});

										//更新变量
										currentImage = indexImage;
										currentIndex = index;

										//移除并添加高亮
										$('#ThumbPic img').removeClass('active');
										$($('#ThumbPic img')[index]).addClass('active');

										//设置向左向右鼠标手势区域的高度                        
										//var tempHeight = $($('#OriginalPic img')[index]).height();
										//$('#aPrev').height(tempHeight);
										//$('#aNext').height(tempHeight);                        
									}	
								}

								//下一张
								function ShowNext(){
									var len = $('#ldh-photoAlbum-pic img').length;
									var next = currentIndex < (len - 1) ? currentIndex + 1 : 0;
									showImage(next);
								}

								//上一张
								function ShowPrep(){
									var len = $('#ldh-photoAlbum-pic img').length;
									var next = currentIndex == 0 ? (len - 1) : currentIndex - 1;
									showImage(next);
								}

								//下一张事件
								$("#aNext").click(function(){
									ShowNext();
									if($(".active").position().left >= 144 * 5){
										$("#btnNext").click();
									}
								});

								//上一张事件
								$("#aPrev").click(function(){
									ShowPrep();
									if($(".active").position().left <= 144 * 5){
										$("#btnPrev").click();
									}
								});

								//初始化事件
								$(".ldh-photoAlbum-border").ready(function(){
									ShowNext();

									//绑定缩略图点击事件
									$('#ThumbPic li').bind('click',function(e){
										var count = $(this).attr('rel');
										showImage(parseInt(count) - 1);
									});
								});
								
							}
							
		
					
						}
						
						
					}))().start();
		
		
	};
	
	
});

